<?php
if (preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $_POST["username"]) & preg_match('/^[a-zA-Z0-9]{5,15}$/', $_POST["password"]) & preg_match('/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]{1,15}+$/u', $_POST["nickname"])) {

	if ($_POST['username'] != "") {
		include 'setting.php';
		$con = mysqli_connect($sqldatabase, $sqlusername, $sqlpassword,$sqltable);
		mysqli_query($con,'set names utf8'); 
		$result = mysqli_query($con,"SELECT * FROM webset where name='tablenum'");
		$uid = 0;
		while ($row = mysqli_fetch_array($result)) {
			$uid = (int)$row["val"] + 1;
		}
		//echo ("<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><script type=\"text/javascript\">alert(\"".$uid."\");</script>");

		mysqli_query($con,"UPDATE webset SET val = '" . $uid . "' WHERE name = 'tablenum'");
		$result = mysqli_query($con,"SELECT * FROM userdata where username='" . trim($_POST['username']) . "'");
		$is_reg = false;
		while ($row = mysqli_fetch_array($result)) {
			$is_reg = true;
		}
		if ($is_reg) {
			echo("<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><script type=\"text/javascript\">alert(\"注册失败，邮箱已经被注册\");window.location.href='http://'+document.domain+'/action/register.php';</script>");
		} else {
			$checkcode = md5($_POST["username"] . "dogmaclient!!!!!q6q6" . time() . rand());
			mysqli_query($con,"INSERT INTO softset (uid,err_z,err_o,err_z_lic,err_o_lic,err_t_lic) VALUES ('" . $uid . "','当前为最新版本', '发现新版本', '授权验证成功', '授权验证失败，授权被停用','授权验证失败，授权不存在')");
			mysqli_query($con,"INSERT INTO userdata (uid,username,password,nickname,checkcode) VALUES ('" . $uid . "','" . $_POST["username"] . "', '" . $_POST["password"] . "', '" . $_POST["nickname"] . "', '" . $checkcode . "')");
			setcookie("nickname", $_POST["nickname"], time() + 180000, "/");
			setcookie("check", $checkcode, time() + 180000, "/");
			echo("<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><script type=\"text/javascript\">window.location.href='http://'+document.domain;</script>");
		}
		mysqli_close($con);
	} else {
		echo("<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><script type=\"text/javascript\">alert(\"注册失败，参数错误\");window.location.href='http://'+document.domain+'/action/register.php';</script>");
	}
} else {
	echo("<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><script type=\"text/javascript\">alert(\"注册失败，账号、密码或者昵称格式不正确\");window.location.href='http://'+document.domain+'/action/register.php';</script>");

}
?>
